﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;

namespace 象棋图片解析
{
    class Program
    {
        static Dictionary<string, string> dictionary = new();

        static void Main(string[] args)
        {

            DirectoryInfo directory = new DirectoryInfo("../../../棋子图片");
            foreach(var file in directory.GetFiles())
            {
                LoadImage(file.FullName, file.FullName.Replace(".png","").Replace("\n","").Replace("\r","").Split('\\').Last());
            }
            Console.WriteLine("数量:" + dictionary.Count);

            string[,] result = ConvertToArray("../../../待解析图片/1.png");
            Display(result);

        }

        static void LoadImage(string src, string name)
        {
            #pragma warning disable CA1416 // 验证平台兼容性
            Bitmap bitmap = new(src);
            #pragma warning restore CA1416 // 验证平台兼容性
            //Console.WriteLine(bitmap);

            for (int i = 0; i < 10; i++)
            {
                for (int j = 0; j < 9; j++)
                {
                    int x_s = i * 50 + 25;
                    int x_e = i * 50 + 75;
                    int y_s = j * 50 + 25;
                    int y_e = j * 50 + 75;

                    List<int> nums = new();

                    for (int x = x_s; x <= x_e; x++)
                    {
                        for (int y = y_s; y <= y_e; y++)
                        {
                            #pragma warning disable CA1416 // 验证平台兼容性
                            Color c = bitmap.GetPixel(y, x);
                            #pragma warning restore CA1416 // 验证平台兼容性
                            nums.Add(c.R);
                            nums.Add(c.G);
                            nums.Add(c.B);
                        }
                    }

                    try
                    {
                        dictionary.Add(string.Join(",", nums), name);
                    }
                    catch
                    {

                    }
                }
            }
        }

        static string[,] ConvertToArray(string src)
        {
            Bitmap bitmap = new(src);
            string[,] result = new string[10,9];
            for (int i = 0; i < 10; i++)
            {
                for (int j = 0; j < 9; j++)
                {
                    int x_s = i * 50 + 25;
                    int x_e = i * 50 + 75;
                    int y_s = j * 50 + 25;
                    int y_e = j * 50 + 75;

                    List<int> nums = new();

                    for (int x = x_s; x <= x_e; x++)
                    {
                        for (int y = y_s; y <= y_e; y++)
                        {
#pragma warning disable CA1416 // 验证平台兼容性
                            Color c = bitmap.GetPixel(y, x);
#pragma warning restore CA1416 // 验证平台兼容性
                            nums.Add(c.R);
                            nums.Add(c.G);
                            nums.Add(c.B);
                        }
                    }

                    string key = string.Join(",", nums);

                    try
                    {
                        result[i, j] = dictionary[key];
                    }
                    catch
                    {

                    }
                }
            }
            return result;
        }

        static void Display(string[,] result)
        {
            for (int i = 0; i < 10; i++)
            {
                for (int j = 0; j < 9; j++)
                {
                    if (result[i, j] == "空")
                    {
                        Console.Write("..");
                    }
                    else
                    {
                        Console.Write(result[i, j]);
                    }
                }
                Console.WriteLine();
            }
        }
    }
}
